async function submitForm() {
  const translateMode = document.getElementById('translateMode').value;
  const fromLanguage = document.getElementById('fromLanguage').value;
  const toLanguage = document.getElementById('toLanguage').value;
  const inputElement = document.getElementById(translateMode === 'image' ? 'inputImage' : 'inputText');
  const formData = new FormData();

  formData.append(translateMode === 'image' ? 'imageData' : 'inputText', inputElement.files ? inputElement.files[0] : inputElement.value);
  formData.append('translateMode', translateMode);
  formData.append('fromLanguage', fromLanguage);
  formData.append('toLanguage', toLanguage);

  try {
    const response = await fetch(translateMode === 'image' ? 'http://localhost:5000/saveImage' : 'http://localhost:5000/saveText', {
      method: 'POST',
      body: formData,
      headers: {
        'Accept': 'application/json'
      }
    });

    if (response.ok) {
      const answer = await response.json();
      document.getElementById('outputText').value = answer.result.trans_result[0].dst;
      if (translateMode === 'image') {
        document.getElementById('inputText').value = answer.result.trans_result[0].src;
      }

      // 保存翻译历史
      const history = getHistory();
      const newItem = {
        src: inputElement.value, // 如果是图片翻译，这里应该是OCR识别的文本
        dst: answer.result.trans_result[0].dst,
        time: new Date().toISOString() // 当前时间
      };
      history.push(newItem);
      saveHistory(history);

    } else {
      throw new Error('Request failed with status: ' + response.status);
    }
  } catch (error) {
    console.error('Error:', error.message || error);
  }
}

// 获取翻译历史
function getHistory() {
  const historyStr = localStorage.getItem('translationHistory');
  if (historyStr) {
    return JSON.parse(historyStr);
  }
  return [];
}

// 存储翻译历史
function saveHistory(history) {
  localStorage.setItem('translationHistory', JSON.stringify(history));
}